/**
 */
package com.dingpeikeji.quickdeer.core.modules.schedef.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.bus.service.TCompBusService;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.driver.service.TDriversService;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefBoardDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefOffDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefVehiDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedef;
import com.dingpeikeji.quickdeer.core.modules.schedef.service.TSchedefService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.DictUtils;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.core.modules.tschedef.entity.TSchedefDrvs;
import com.dingpeikeji.quickdeer.core.modules.tschedef.service.TSchedefDrvsService;
import com.dingpeikeji.quickdeer.core.modules.viewspot.entity.SysViewspot;
import com.dingpeikeji.quickdeer.core.modules.viewspot.service.SysViewspotService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import test.numberUtil;

/**
 * 班次定义Controller
 * 
 * @author Zhaoy
 * @version 2016-05-27
 */
@Controller
@RequestMapping(value = "${adminPath}/schedef/tSchedefModify")
public class TSchedefModifyController extends BaseController {

	@Autowired
	private TSchedefService tSchedefService;
	@Autowired
	private TSchedefDrvsService tSchedefDriversservice;
	@Autowired
	private TSitedtlService tSitedtlservice;
	@Autowired
	private TCompBusService compBusService;
	@Autowired
	private SysViewspotService viewsService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private TLineService tLineService;
	@Autowired
	private SysViewspotService sysViewspotService;

	@ModelAttribute
	public TSchedef get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return tSchedefService.get(id);
		} else {
			return new TSchedef();
		}
	}

	@RequiresPermissions("schedef:tSchedefModify:view")
	@RequestMapping(value = { "list", "" })
	public String list(TSchedef tSchedef, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (tSchedef.getOfficeid() == null)
			tSchedef.setOfficeid(UserUtils.getUser().getOffice().getId());
		// 只能读取已经审核通过的班次信息
		tSchedef.setAuditstatus("1");
		Page<TSchedef> page = tSchedefService.findPage(new Page<TSchedef>(request, response), tSchedef);
		model.addAttribute("page", page);
		return "modules/schedef/tSchedefModifyList";

	}

	// 通过车牌号 查询汽车信息
	@RequestMapping(value = "ajaxList")
	@ResponseBody
	public List<TCompBus> ajaxList(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 通过officeid 查询客运公司巴士信息 AJAX
		String officeids = request.getParameter("officeids");
		String licno = request.getParameter("licno");
		TCompBus bus = new TCompBus();
		Office office = new Office();
		office.setId(officeids);
		bus.setOffice(office);
		bus.setLicenseno(licno);
		return compBusService.findList(bus);
	}

	// 通过客运公司查询当前公司下所有的车牌号
	@RequestMapping(value = "ajaxVehiclename")
	@ResponseBody
	public List<TCompBus> ajaxVehiclename(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 通过officeid 查询客运公司巴士信息 AJAX
		String officeids = request.getParameter("officeids");
		return compBusService.findListOffice(officeids);
	}

	// 通过城市查询上下客点
	@RequestMapping(value = "ajaxStied")
	@ResponseBody
	public List<TSitedtl> ajaxStied(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 通过城市编号 查询对应的景点
		String code = request.getParameter("code");
		if (code != null) {
			TSitedtl sitedtl = new TSitedtl();
			sitedtl.setCitycode(code);
			System.out.println("查询出来的上下客点信息============" + tSitedtlservice.findList(sitedtl));
			return tSitedtlservice.findList(sitedtl);
		}
		return null;
	}

	// 通过城市查询景点
	@RequestMapping(value = "ajaxSpot")
	@ResponseBody
	public List<SysViewspot> ajaxSpot(HttpServletRequest request, HttpServletResponse response, Model model) {
		// 通过城市编号 查询对应的景点
		String code = request.getParameter("code");
		if (code != null) {
			SysViewspot viewspot = new SysViewspot();
			viewspot.setCitycode(code);
			System.out.println("查询出来的景点信息============" + sysViewspotService.findList(viewspot));
			return sysViewspotService.findList(viewspot);
		}
		return null;
	}

	@RequiresPermissions("schedef:tSchedefModify:view")
	@RequestMapping(value = "form")
	public String form(TSchedef tSchedef, Model model) {
		Date tomorrow = new Date();
		tomorrow.setDate(tomorrow.getDate() + 1);
		model.addAttribute("tSchedef", tSchedef);
		model.addAttribute("tomorrow", DateUtils.formatDate(tomorrow, "yyyy-MM-dd"));

		// 查询所有站点
		TSitedtl sitedtl = new TSitedtl();
		SysViewspot sysViewspot = new SysViewspot();
		TLine tLine = new TLine();
		model.addAttribute("tlinelist", tLineService.findList(tLine));
		model.addAttribute("tSitedList", tSitedtlservice.findListAll());
		model.addAttribute("viewSpotlist", viewsService.findList(sysViewspot));
		// model.addAttribute("viewSpot",tSchedef.getViewSpot());
		model.addAttribute("board", tSchedef.gettSchedefBoardList());
		model.addAttribute("off", tSchedef.gettSchedefOffList());
		model.addAttribute("veh", tSchedef.getTSchedefVehiList());
		// 暂时得到景点的城市名 后期再删掉处理

		// model.addAttribute("tline", tSchedef.getTline());
		// 获取发车日期类型 进行切割
		String str = tSchedef.getDeparttype();
		if (str != null) {
			String[] strs = str.split("_");
			for (int i = 0; i < strs.length; i++) {
				model.addAttribute("departtypes1", strs[0]);
				model.addAttribute("departtypes2", strs[1]);
				model.addAttribute("departtypes3", strs[2]);
				model.addAttribute("departtypes4", strs[3]);
				model.addAttribute("departtypes5", strs[4]);
				model.addAttribute("departtypes6", strs[5]);
				model.addAttribute("departtypes7", strs[6]);
				model.addAttribute("departtypes8", strs[7]);
			}
		}

		// 将上客点 信息通过json传递给页面
		/*
		 * JSONArray tSchedefBoardJson = new JSONArray(); for (int i = 0; i <
		 * tSchedef.getTSchedefBoardList().size(); i++) { JSONObject jo = new
		 * JSONObject(); jo.put("boardseqno",
		 * tSchedef.getTSchedefBoardList().get(i).getSeqno());
		 * jo.put("boardaddr",
		 * tSchedef.getTSchedefBoardList().get(i).getBoardaddr());
		 * jo.put("boardtime",
		 * tSchedef.getTSchedefBoardList().get(i).getBoardtime());
		 * tSchedefBoardJson.add(jo); } model.addAttribute("tSchedefBoardJson",
		 * tSchedefBoardJson); // 将下客点 信息通过json传递给页面 JSONArray tSchedefOffJson =
		 * new JSONArray(); for (int i = 0; i <
		 * tSchedef.gettSchedefOffList().size(); i++) { JSONObject jo = new
		 * JSONObject(); jo.put("offseqno",
		 * tSchedef.gettSchedefOffList().get(i).getSeqno()); jo.put("offaddr",
		 * tSchedef.gettSchedefOffList().get(i).getOffaddr());
		 * tSchedefOffJson.add(jo); } model.addAttribute("tSchedefOffJson",
		 * tSchedefOffJson); model.addAttribute("tSchedefVehi",
		 * JSON.toJSONString(tSchedef.getTSchedefVehiList()));
		 * System.out.println("上客点==========" + tSchedefBoardJson);
		 * System.out.println("下客点==========" + tSchedefOffJson);
		 * System.out.println("巴士==========" + tSchedef.getTSchedefVehiList());
		 */
		// 查出所有客运公司
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Office qryOffice = new Office();
		qryOffice.setFlatType(Office.FLAT_TYPE_BUS);
		List<Office> list = officeService.findListByTerm(qryOffice);
		for (int i = 0; i < list.size(); i++) {
			Office e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentId());
			map.put("pIds", e.getParentIds());
			map.put("name", e.getName());
			mapList.add(map);
		}
		model.addAttribute("officelist", mapList);
		return "modules/schedef/tSchedefModifyForm";
	}

	@RequiresPermissions("schedef:tSchedef:edit")
	@RequestMapping(value = "save")
	public String save(TSchedef tSchedef, Model model, RedirectAttributes redirectAttributes,
			HttpServletRequest request) {
		if (!beanValidator(model, tSchedef)) {
			return form(tSchedef, model);
		}
		numberUtil numberUtil = new numberUtil();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmm");
		Date date = new Date();
		// tSchedef.setAuditstatus("2");
		tSchedef.setSchecode(formatter.format(date));
		// tSchedef.setCanbeuse("1");
		// tSchedef.setInitstatus("1");
		tSchedef.setOfficeid(UserUtils.getUser().getOffice().getId());
		tSchedef.setCompanyid(UserUtils.getUser().getCompany().getId());
		// 设置操作人为当前登录用户
		tSchedef.setCreateby(UserUtils.getUser().getId());
		// 为城市赋值
		String departcitys = request.getParameter("departcitys");
		String destcitys = request.getParameter("destcitys");
		// 当departcitycode不为空时 即是通过线路选择定义的 城市的
		/*
		 * if(!request.getParameter("departcitycode").equals("")){
		 * tSchedef.setDepartcity(request.getParameter("departcitycode"));
		 * tSchedef.setDestcity(request.getParameter("destcitycode")); }else
		 * {//当departcitycode为空时 即是通过自定义选择的城市
		 * tSchedef.setDepartcity(departcitys); tSchedef.setDestcity(destcitys);
		 * }
		 */

		tSchedefService.save(tSchedef);
		/*
		 * TSchedefDrvs drvs = new TSchedefDrvs();
		 * drvs.setDefvehiid(tSchedef.getId()); drvs.setDefdrvid(defdrvid);
		 */
		addMessage(redirectAttributes, "调度班次成功");
		return "redirect:" + Global.getAdminPath() + "/schedef/tSchedefModify/?repage";
	}

	@RequiresPermissions("schedef:tSchedef:edit")
	@RequestMapping(value = "delete")
	public String delete(TSchedef tSchedef, RedirectAttributes redirectAttributes) {
		tSchedef.setDelflag("1");
		tSchedefService.update(tSchedef);
		addMessage(redirectAttributes, "停用班次成功");
		return "redirect:" + Global.getAdminPath() + "/schedef/tSchedefModify/?repage";
	}

}
